package com.hjy.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hjy.demo.mapper.UserMapper;
import com.hjy.demo.pojo.User;
import com.hjy.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class UserServiceImpl implements UserService {
    @Resource
    private UserMapper userMapper;

    @Override
    public User findUserByName(String name) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("usr_name",name);
        return userMapper.selectOne(wrapper);
    }

    @Override
    public User login(String name, String pwd) {
        User u = findUserByName(name);
        if (u.getUsrPassword().equals(pwd)){
            return u;
        }
        return null;

    }

    @Override
    public IPage<User> selectPage(Page<User> page, Wrapper<User> wrapper) {
        IPage<User> userIPage = userMapper.selectPage(page,wrapper);
        return userIPage;
    }

    @Override
    public int addUser(User user) {
        return userMapper.insert(user);
    }

    @Override
    public int deleteUser(Long usrId) {
        return userMapper.deleteById(usrId);
    }

    @Override
    public int updateUser(User user) {
        return userMapper.updateById(user);
    }

    @Override
    public User getUser(Long usrId) {
        return userMapper.selectById(usrId);
    }
}
